package com.tianfei.crowd.service.impl;

import com.tianfei.crowd.entity.po.AddressPO;
import com.tianfei.crowd.entity.po.AddressPOExample;
import com.tianfei.crowd.entity.po.OrderPO;
import com.tianfei.crowd.entity.po.OrderProjectPO;
import com.tianfei.crowd.entity.vo.AddressVO;
import com.tianfei.crowd.entity.vo.OrderProjectVO;
import com.tianfei.crowd.entity.vo.OrderVO;
import com.tianfei.crowd.mapper.AddressPOMapper;
import com.tianfei.crowd.mapper.OrderPOMapper;
import com.tianfei.crowd.mapper.OrderProjectPOMapper;
import com.tianfei.crowd.service.api.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Herz
 * @date: 2021/8/9 20:00
 */
@Transactional(readOnly = true)
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderProjectPOMapper orderProjectPOMapper;

    @Autowired
    private AddressPOMapper addressPOMapper;

    @Autowired
    private OrderPOMapper orderPOMapper;



    @Override
    public OrderProjectVO selectOrderProjectVO(Integer returnId) {

       OrderProjectVO orderProjectVO = orderProjectPOMapper.selectOrderProjectVO(returnId);

        return orderProjectVO;
    }



    @Override
    public List<AddressVO> selectAddressVO(Integer memberId) {

        AddressPOExample example = new AddressPOExample();
        AddressPOExample.Criteria criteria = example.createCriteria();
        criteria.andMemberIdEqualTo(memberId);

        List<AddressPO> addressPOList = addressPOMapper.selectByExample(example);

        List<AddressVO> addressVOList = new ArrayList<>();

        for (AddressPO addressPO : addressPOList){

            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(addressPO, addressVO);

            addressVOList.add(addressVO);
        }

        return addressVOList;
    }


    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public void saveAddressVO(AddressVO addressVO) {

        AddressPO addressPO = new AddressPO();
        BeanUtils.copyProperties(addressVO, addressPO);

        addressPOMapper.insert(addressPO);

    }


    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public void saveOrder(OrderVO orderVO) {

        OrderPO orderPO = new OrderPO();
        BeanUtils.copyProperties(orderVO, orderPO);

        OrderProjectVO orderProjectVO = orderVO.getOrderProjectVO();
        OrderProjectPO orderProjectPO = new OrderProjectPO();
        BeanUtils.copyProperties(orderProjectVO, orderProjectPO);

        // 保存 orderPO 自动生成的主键，也就是 orderProjectPO 的外键
        orderPOMapper.insert(orderPO);

        Integer orderId = orderPO.getId();
        orderProjectPO.setOrderId(orderId);

        orderProjectPOMapper.insert(orderProjectPO);

    }
}
